package worker

import (
	"bytes"
	"github.com/PuerkitoBio/goquery"
	"io/ioutil"
	"net/http"
)

type Helper struct {}

// 爬取页面，返回dom对象
func (h *Helper) Dom (url string) (*goquery.Document, error) {
	client := &http.Client{}
	req, _ := http.NewRequest("GET", url, nil)
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36")
	resp, _ := client.Do(req)
	defer resp.Body.Close()

	// 解析内容
	getByte, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}
	reader := bytes.NewReader(getByte)
	dom, err := goquery.NewDocumentFromReader(reader)
	if err != nil {
		return nil, err
	}
	return dom, nil
}

